package com.ll.studentservice.service.impl;

import com.ll.servicebase.exceptionhandler.LLException;
import com.ll.studentservice.entity.EduKe;
import com.ll.studentservice.entity.EduKeInfo;
import com.ll.studentservice.entity.EduKeTime;
import com.ll.studentservice.entity.vo.EduKeTimeVo;
import com.ll.studentservice.mapper.EduKeMapper;
import com.ll.studentservice.service.EduKeInfoService;
import com.ll.studentservice.service.EduKeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ll.studentservice.service.EduKeTimeService;
import org.apache.ibatis.annotations.One;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ll
 * @since 2020-11-27
 */
@Service
public class EduKeServiceImpl extends ServiceImpl<EduKeMapper, EduKe> implements EduKeService {

    @Autowired
    private EduKeTimeService eduKeTimeService;

    @Autowired
    private EduKeInfoService eduKeInfoService;

//    @Test
//    public void test() {
//        int value = LocalDate.now().getDayOfWeek().getValue();
//        System.out.println(value);
//
//    }

    //获取将要进行签到课程的id(ke_info_id)
    @Override
    public String findKeInfoId(String classId) {
        //获取当前星期几
        int weekTest = LocalDate.now().getDayOfWeek().getValue();

        //转为String类型
        String week = String.valueOf(weekTest);

        //用于设置查询数据库哪个字段,第几节课的意思,No.Course
        String noCourse = this.findNoAutoSignCourseNumber();

        if (StringUtils.isEmpty(noCourse)){
            noCourse = "no";
        }

        //测试使用，设置上第几节课
        //noCourse = "two";

        //自定义sql语句
        //SELECT one FROM edu_ke WHERE class_id = 1331956305882161153 AND week = 1;
        String eduKeInfoId = baseMapper.getNowCourseId(noCourse, classId, week);

        return eduKeInfoId;
    }

    //获取当前时间将要进行第几节课的签到,或者说要上第节课
    //返回 one two three (表示第几节课)
    @Override
    public String findCourseNumber() {
        //获取第一节到第九节课程的时间
        EduKeTime eduKeTime = eduKeTimeService.getById(1);
        EduKeTimeVo eduKeTimeVo = new EduKeTimeVo();
        BeanUtils.copyProperties(eduKeTime, eduKeTimeVo);

        //08:10:00-08:55:00
        String oneCourseStartTime = eduKeTimeVo.getOneTime().split("-")[0];     //第一节课开始时间
        String oneCourseEndTime = eduKeTimeVo.getOneTime().split("-")[1];       //第一节课结束时间
        String twoCourseStartTime = eduKeTimeVo.getTwoTime().split("-")[0];     //第二节课开始时间
        String threeCourseStartTime = eduKeTimeVo.getThreeTime().split("-")[0];
        String fourCourseStartTime = eduKeTimeVo.getFourTime().split("-")[0];
        String fiveCourseStartTime = eduKeTimeVo.getFiveTime().split("-")[0];
        String sixCourseStartTime = eduKeTimeVo.getSixTime().split("-")[0];
        String sevenCourseStartTime = eduKeTimeVo.getSevenTime().split("-")[0];
        String eightCourseStartTime = eduKeTimeVo.getEightTime().split("-")[0];
        String nightCourseStartTime = eduKeTimeVo.getNightTime().split("-")[0];

        //获取现在的时间
        LocalTime now = LocalTime.now();

        //测试,手动设置当前时间
//        now = LocalTime.parse("16:30:00");

        //用于设置查询数据库哪个字段,第几节课的意思,No.Course
        String noCourse = "";

        //判断是否可以签到
        //优化 将签到时间限制在上课前十分钟内
        if (now.isBefore(LocalTime.parse(oneCourseStartTime))){
            noCourse = "one";
            System.out.println("现在可以开始第一节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(twoCourseStartTime))){
            noCourse = "two";
            System.out.println("现在可以开始第二节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(threeCourseStartTime))){
            noCourse = "three";
            System.out.println("现在可以开始第三节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(fourCourseStartTime))){
            noCourse = "four";
            System.out.println("现在可以开始第四节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(fiveCourseStartTime))){
            noCourse = "five";
            System.out.println("现在可以开始第五节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(sixCourseStartTime))){
            noCourse = "six";
            System.out.println("现在可以开始第六节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(sevenCourseStartTime))){
            noCourse = "seven";
            System.out.println("现在可以开始第七节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(eightCourseStartTime))){
            noCourse = "eigth";
            System.out.println("现在可以开始第八节课签到");
            return noCourse;
        } else if (now.isBefore(LocalTime.parse(nightCourseStartTime))){
            noCourse = "night";
            System.out.println("现在可以开始第九节课签到");
            return noCourse;
        } else {
            noCourse = "no";
            System.out.println("当前时间,没有要签到的课程");
            throw new LLException(20001,"当前时间,没有要签到的课程");
        }
    }

    @Test
    public void test() {
        LocalTime now = LocalTime.now();
//        now.isAfter(LocalTime.parse("08:10:00").minusMinutes(10)) &&
        //now.isBefore(LocalTime.parse("08:55:00"));
//
//        LocalTime parse = LocalTime.parse("08:00:00");
//        boolean before = parse.isBefore(LocalTime.parse("08:55:00"));
//        System.out.println(before);

//        now.isAfter(LocalTime.parse("08:10:00").minusMinutes(10))
//        LocalTime localTime = LocalTime.parse("08:10:00").minusMinutes(10);
//        System.out.println(localTime);
    }

    //获取当前时间将要进行第几节课的签到,或者说要上第节课
    //返回 one two three (表示第几节课)
    @Override
    public String findNoAutoSignCourseNumber() {
        //获取第一节到第九节课程的时间
        EduKeTime eduKeTime = eduKeTimeService.getById(1);
        EduKeTimeVo eduKeTimeVo = new EduKeTimeVo();
        BeanUtils.copyProperties(eduKeTime, eduKeTimeVo);


        //08:10:00-08:55:00
        String oneCourseStartTime = eduKeTimeVo.getOneTime().split("-")[0];     //第一节课开始时间
        String oneCourseEndTime = eduKeTimeVo.getOneTime().split("-")[1];       //第一节课结束时间

        String twoCourseStartTime = eduKeTimeVo.getTwoTime().split("-")[0];     //第二节课开始时间
        String twoCourseEndTime = eduKeTimeVo.getTwoTime().split("-")[1];

        String threeCourseStartTime = eduKeTimeVo.getThreeTime().split("-")[0];
        String threeCourseEndTime = eduKeTimeVo.getThreeTime().split("-")[1];

        String fourCourseStartTime = eduKeTimeVo.getFourTime().split("-")[0];
        String fourCourseEndTime = eduKeTimeVo.getFourTime().split("-")[1];

        String fiveCourseStartTime = eduKeTimeVo.getFiveTime().split("-")[0];
        String fiveCourseEndTime = eduKeTimeVo.getFiveTime().split("-")[1];

        String sixCourseStartTime = eduKeTimeVo.getSixTime().split("-")[0];
        String sixCourseEndTime = eduKeTimeVo.getSixTime().split("-")[1];
        String sevenCourseStartTime = eduKeTimeVo.getSevenTime().split("-")[0];
        String sevenCourseEndTime = eduKeTimeVo.getSevenTime().split("-")[1];
        String eightCourseStartTime = eduKeTimeVo.getEightTime().split("-")[0];
        String eightCourseEndTime = eduKeTimeVo.getEightTime().split("-")[1];

        String nightCourseStartTime = eduKeTimeVo.getNightTime().split("-")[0];
        String nightCourseEndTime = eduKeTimeVo.getNightTime().split("-")[1];

        //获取现在的时间
        LocalTime now = LocalTime.now();

        //测试,手动设置当前时间
//        now = LocalTime.parse("14:25:00");

        //设置可以手动考勤的时间是每节课开始时间前10分钟到每节课结束时间这段时间
        //08:00-08:55
        if (now.isAfter(LocalTime.parse(oneCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(oneCourseEndTime))){
            System.out.println("现在可以开始第一节课签到");
            return "one";

            //08:55-09:50
        } else if (now.isAfter(LocalTime.parse(twoCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(twoCourseEndTime))){
            System.out.println("现在可以开始第二节课签到");
            return "two";

            //10:00-10:55
        } else if (now.isAfter(LocalTime.parse(threeCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(threeCourseEndTime))){
            System.out.println("现在可以开始第三节课签到");
            return "three";

            //10:55-11:50
        } else if (now.isAfter(LocalTime.parse(fourCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(fourCourseEndTime))){
            System.out.println("现在可以开始第四节课签到");
            return "four";

            //14:20:00-15:15:00
        } else if (now.isAfter(LocalTime.parse(fiveCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(fiveCourseEndTime))){
            System.out.println("现在可以开始第五节课签到");
            return "five";

            //15:15:00-16:10:00
        } else if (now.isAfter(LocalTime.parse(sixCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(sixCourseEndTime))){
            System.out.println("现在可以开始第六节课签到");
            return "six";

            //16:10:00-17:05:00
        } else if (now.isAfter(LocalTime.parse(sevenCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(sevenCourseEndTime))){
            System.out.println("现在可以开始第七节课签到");
            return "seven";

            //17:05:00-18:00:00
        } else if (now.isAfter(LocalTime.parse(eightCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(eightCourseEndTime))){
            System.out.println("现在可以开始第八节课签到");
            return "eight";

            //19:20:00-21:10:00
        } else if (now.isAfter(LocalTime.parse(nightCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(nightCourseEndTime))){
            System.out.println("现在可以开始第九节课签到");
            return "night";
        } else {
           return "";
        }
    }

    //获取当前时间将要进行第几节课的签到,或者说要上第节课
    //返回 one two three (表示第几节课)
    //不易理解弃用
//    @Override
//    public String findNoAutoSignCourseNumber() {
//        //获取第一节到第九节课程的时间
//        EduKeTime eduKeTime = eduKeTimeService.getById(1);
//        EduKeTimeVo eduKeTimeVo = new EduKeTimeVo();
//        BeanUtils.copyProperties(eduKeTime, eduKeTimeVo);
//
//        //08:10:00-08:55:00
//        String oneCourseStartTime = eduKeTimeVo.getOneTime().split("-")[0];     //第一节课开始时间
//        String oneCourseEndTime = eduKeTimeVo.getOneTime().split("-")[1];       //第一节课结束时间
//        String twoCourseStartTime = eduKeTimeVo.getTwoTime().split("-")[0];     //第二节课开始时间
//        String twoCourseEndTime = eduKeTimeVo.getTwoTime().split("-")[1];
//        String threeCourseStartTime = eduKeTimeVo.getThreeTime().split("-")[0];
//        String threeCourseEndTime = eduKeTimeVo.getThreeTime().split("-")[1];
//        String fourCourseStartTime = eduKeTimeVo.getFourTime().split("-")[0];
//        String fourCourseEndTime = eduKeTimeVo.getFourTime().split("-")[1];
//        String fiveCourseStartTime = eduKeTimeVo.getFiveTime().split("-")[0];
//        String fiveCourseEndTime = eduKeTimeVo.getFiveTime().split("-")[1];
//        String sixCourseStartTime = eduKeTimeVo.getSixTime().split("-")[0];
//        String sixCourseEndTime = eduKeTimeVo.getSixTime().split("-")[1];
//        String sevenCourseStartTime = eduKeTimeVo.getSevenTime().split("-")[0];
//        String sevenCourseEndTime = eduKeTimeVo.getSevenTime().split("-")[1];
//        String eightCourseStartTime = eduKeTimeVo.getEightTime().split("-")[0];
//        String eightCourseEndTime = eduKeTimeVo.getEightTime().split("-")[1];
//        String nightCourseStartTime = eduKeTimeVo.getNightTime().split("-")[0];
//        String nightCourseEndTime = eduKeTimeVo.getNightTime().split("-")[1];
//
//        //获取现在的时间
//        LocalTime now = LocalTime.now();
//
//        //测试,手动设置当前时间
//        //now = LocalTime.parse("06:23:00");
//
//        //第一节课开始时间减去十分钟
//        LocalTime oneCourseStartTimeMinus10 = LocalTime.parse(oneCourseStartTime).minusMinutes(10);
//
//        //在第一节课开始前十分钟并且在第一节课结束前可以进行第一节课的手动签到,适用于突击考勤
//        //08:00-08:55
//        if (now.isAfter(oneCourseStartTimeMinus10) && now.isBefore(LocalTime.parse(oneCourseEndTime))){
//            System.out.println("现在可以开始第一节课签到");
//            return "one";
//
//            //在第一节课结束后并且在第二节课结束前可以进行第二节课的手动签到,适用于突击考勤
//            //08:55-09:50
//        } else if (now.isAfter(LocalTime.parse(oneCourseEndTime)) && now.isBefore(LocalTime.parse(twoCourseEndTime))){
//            System.out.println("现在可以开始第二节课签到");
//            return "two";
//
//            //09:50-10:55
//            //在第二节课结束后并且在第三节课结束前可以进行第三节课的手动签到,适用于突击考勤
//        } else if (now.isAfter(LocalTime.parse(twoCourseEndTime)) && now.isBefore(LocalTime.parse(threeCourseEndTime))){
//            System.out.println("现在可以开始第三节课签到");
//            return "three";
//
//            //10:55-11:50
//        } else if (now.isAfter(LocalTime.parse(threeCourseEndTime)) && now.isBefore(LocalTime.parse(fourCourseEndTime))){
//            System.out.println("现在可以开始第四节课签到");
//            return "four";
//
//            // 11:05:00-11:50:00 第四节课开始结束时间
//            // 14:30:00-15:15:00 第五节课开始结束时间
//            // 第五节课开始时间由于距离第四节课结束时间相差过多时间,单独设置时间判断
//            // 使用第五节课的开始时间前10分钟,作为第一个参数
//        } else if (now.isAfter(LocalTime.parse(fiveCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(fiveCourseEndTime))){
//            System.out.println("现在可以开始第五节课签到");
//            return "five";
//        } else if (now.isAfter(LocalTime.parse(fiveCourseEndTime)) && now.isBefore(LocalTime.parse(sixCourseEndTime))){
//            System.out.println("现在可以开始第六节课签到");
//            return "six";
//        } else if (now.isAfter(LocalTime.parse(sixCourseEndTime)) && now.isBefore(LocalTime.parse(sevenCourseEndTime))){
//            System.out.println("现在可以开始第七节课签到");
//            return "seven";
//        } else if (now.isAfter(LocalTime.parse(sevenCourseEndTime)) && now.isBefore(LocalTime.parse(eightCourseEndTime))){
//            System.out.println("现在可以开始第八节课签到");
//            return "eigth";
//
//            // 15:15:00-18:00:00 第八节课开始结束时间
//            // 19:30:00-21:10:00 第九节课开始结束时间
//            // 第九节课开始时间由于距离第八节课结束时间相差过多时间,单独设置时间判断
//            //使用第九节课的开始时间前10分钟,作为第一个参数
//        } else if (now.isAfter(LocalTime.parse(nightCourseStartTime).minusMinutes(10)) && now.isBefore(LocalTime.parse(nightCourseEndTime))){
//            System.out.println("现在可以开始第九节课签到");
//            return "night";
//        } else {
//            System.out.println("当前时间,没有要签到的课程");
//            throw new LLException(20001,"当前时间,没有要签到的课程");
//        }
//    }


    //获取将要签到的课程信息(测试使用)
    @Override
    public EduKeInfo findlaterCourseTest(String classId, String week, String nowTime) {

        EduKeTime eduKeTime = eduKeTimeService.getById(1);
        EduKeTimeVo eduKeTimeVo = new EduKeTimeVo();
        BeanUtils.copyProperties(eduKeTime, eduKeTimeVo);

        String oneCourseStartTime = eduKeTimeVo.getOneTime().split("-")[0];     //第一节课开始时间
        String oneCourseEndTime = eduKeTimeVo.getOneTime().split("-")[1];       //第一节课结束时间
        String twoCourseStartTime = eduKeTimeVo.getTwoTime().split("-")[0];     //第二节课开始时间
        String threeCourseStartTime = eduKeTimeVo.getThreeTime().split("-")[0];
        String fourCourseStartTime = eduKeTimeVo.getFourTime().split("-")[0];
        String fiveCourseStartTime = eduKeTimeVo.getFiveTime().split("-")[0];
        String sixCourseStartTime = eduKeTimeVo.getSixTime().split("-")[0];
        String sevenCourseStartTime = eduKeTimeVo.getSevenTime().split("-")[0];
        String eightCourseStartTime = eduKeTimeVo.getEightTime().split("-")[0];
        String nightCourseStartTime = eduKeTimeVo.getNightTime().split("-")[0];

        //获取现在的时间
//        LocalTime now = LocalTime.now();

        //测试,手动设置当前时间
        LocalTime now = LocalTime.parse(nowTime);

        String noCourse = "";

        //判断是否可以签到
        //优化 将签到时间限制在上课前十分钟内
        if (now.isBefore(LocalTime.parse(oneCourseStartTime))){
            noCourse = "one";
            System.out.println("现在可以开始第一节课签到");
        } else if (now.isBefore(LocalTime.parse(twoCourseStartTime))){
            noCourse = "two";
            System.out.println("现在可以开始第二节课签到");
        } else if (now.isBefore(LocalTime.parse(threeCourseStartTime))){
            noCourse = "three";
            System.out.println("现在可以开始第三节课签到");
        } else if (now.isBefore(LocalTime.parse(fourCourseStartTime))){
            noCourse = "four";
            System.out.println("现在可以开始第四节课签到");
        } else if (now.isBefore(LocalTime.parse(fiveCourseStartTime))){
            noCourse = "five";
            System.out.println("现在可以开始第五节课签到");
        } else if (now.isBefore(LocalTime.parse(sixCourseStartTime))){
            noCourse = "six";
            System.out.println("现在可以开始第六节课签到");
        } else if (now.isBefore(LocalTime.parse(sevenCourseStartTime))){
            noCourse = "seven";
            System.out.println("现在可以开始第七节课签到");
        } else if (now.isBefore(LocalTime.parse(eightCourseStartTime))){
            noCourse = "eigth";
            System.out.println("现在可以开始第八节课签到");
        } else if (now.isBefore(LocalTime.parse(nightCourseStartTime))){
            noCourse = "night";
            System.out.println("现在可以开始第九节课签到");
        } else {
            noCourse = "no";
            System.out.println("当前时间,没有要签到的课程");
        }

        //测试使用，设置上第几节课
        //noCourse = "two";

        //自定义sql语句
        //SELECT ${noCourse} FROM edu_ke WHERE class_id = #{classId} AND week = #{week};
        String eduKeInfoId = baseMapper.getNowCourseId(noCourse, classId, week);
        System.out.println("课程id: " + eduKeInfoId);

        //根据课程id查询课程详细信息
        EduKeInfo eduKeInfo = eduKeInfoService.getById(eduKeInfoId);

        return eduKeInfo;
    }
}
